package slxy.jiangtao.dts.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import slxy.jiangtao.dts.core.common.bean.Constants;
import slxy.jiangtao.dts.core.common.service.CommonService;
import slxy.jiangtao.dts.core.util.StringUtil;
import slxy.jiangtao.dts.entity.sys.SystemFiles;
import slxy.jiangtao.dts.entity.sys.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * Code must be sexy
 * Created by Jiang Tao on 2017/4/8.
 */
public class BaseController extends AbstractController {

    /***
     * 日志工具
     */
    private Logger logger = LoggerFactory.getLogger(BaseController.class);
    public static final SimpleDateFormat fm = new SimpleDateFormat("yyyyMMddHHmmss");
    public static final SimpleDateFormat fm2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    protected CommonService commonService;

    /**
     * 获取系统登录的用户
     */
    public User getUserInSession(HttpServletRequest request) {
        return (User) request.getSession().getAttribute(Constants.USER_IN_SESSION);
    }

    /**
     * @param fileNos 以逗号隔开的文件no字符串
     */
    public List<SystemFiles> getFiles(String fileNos) {
        List<SystemFiles> systemFilesList = new ArrayList<>();
        if (StringUtil.isNotEmpty(fileNos)) {
            Arrays.stream(fileNos.substring(1).split(",")).forEach(
                    fileNo -> {
                        SystemFiles systemFiles = commonService.get(SystemFiles.class, fileNo);
                        Optional.ofNullable(systemFiles).ifPresent(
                                systemFile -> systemFilesList.add(systemFile)
                        );
                    }
            );
        }
        return systemFilesList;
    }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        return null;
    }
}
